package omq.filetransfer;

import java.io.IOException;
import java.net.UnknownHostException;

import omq.exception.OMQPropertiesReaderException;
import omq.exception.OMQException;
import omq.api.logger.*;
import omq.utils.*;


import java.util.Timer;
import java.util.TimerTask;

import javax.xml.rpc.ServiceException;

/*
 * OMQ FileTransfer:
 * -----------------
 * 
 * OMQ FileTransfer system is a fully operational file system transfer protocol over OMQ.
 * It sends files to messages in OMQ queue and later receives them back to files.
 *  
 * All system parameters (including OMQ parameters and file-system parameters) are configured using these 2 properties file: 
 * 		for file sender   - omqsend.properties
 * 		for file receiver - omqreceive.properties
 * 
 * Following are the runnable options:
 *    
 * OMQSendFile - sends one specific file specified in parameter omq.ft.filename to a queue.
 * OMQSendFiles - a deamon that wait for new files on the folder omq.ft.path and sends all the files from this folder.
 * OMQReceiveFile - receives only one file from queue into the folder omq.ft.path. 
 * OMQReceiveFiles - a deamon that samples the queue every interval and saves the messages as files. 
 * there are 2 properties file to configure.
 *  
 *  Author  	  	: Hagai Izenberg
 *  Creating Date 	: 20-05-2008
 *  Company 		: OMQI
 */

public class OMQReceiveFiles  {

	/**
	 * @param args
	 * @throws ClassNotFoundException 
	 * @throws IOException 
	 * @throws OMQException 
	 */
	
	OMQPropertiesReader prop;
	boolean success=true;
	OMQFileTransfer sender;

	public static void main(String[] args)
	{

		final String defaultPropertiesFile = "omqreceive.properties";
		String propertiesFile = defaultPropertiesFile;
		
		OMQLogger.getInstance().info("#################################################################################");
		OMQLogger.getInstance().info("# OMQ File Transfer version "+OMQFileTransfer.getVersion()+" - receive files started at "+(FileUtil.time("yyyy.MM.dd 'at' hh:mm:ss")));
		OMQLogger.getInstance().info("#################################################################################");

		if (args.length>0)
		{
			if (args[0].compareToIgnoreCase("-classpath")!= 0)
			{
				propertiesFile = args[0];
				OMQLogger.getInstance().info("using given properties file named "+propertiesFile);
			}
		}
		else
		{
			OMQLogger.getInstance().info("Using default properties file named "+propertiesFile+", you can pass a properties file name as a parameter to this java program");
		}
		
		OMQReceiveFiles receive = new OMQReceiveFiles();
		receive.receiveFiles(propertiesFile);
	}
	
	public void receiveFiles(String propertiesFile)
	{
		try 
		{	
			// ------------------------------------------------------------
			// getting properties
			// ------------------------------------------------------------
			OMQPropertiesReader prop = OMQPropertiesReader.createOMQPropertiesReader(propertiesFile);
			sender = new OMQFileTransfer(prop);

			String ftSleepTime = prop.getProperty("omq.ft.sleeptime");
			OMQLogger.getInstance().info("omq.ft.sleeptime in properties file is set to "+ftSleepTime);
			int iSleepTime = (new Integer(ftSleepTime)).intValue();
			OMQLogger.getInstance().info("sleep time is set to "+iSleepTime+" seconds");

			// ------------------------------------------------------------
			// looping to get files
			// ------------------------------------------------------------
			while (true)
			{
				boolean goon = false;
				try 
				{
					goon = sender.receiveFile();
				}
				catch (OMQFileTransferException e) 
				{
					// TODO Auto-generated catch block
					OMQLogger.getInstance().error("failed to receive file");
					
					success = false;
				}	

				
				OMQLogger.getInstance().info("------------------------------");
				while(goon)
				{
					try 
					{
						goon = sender.receiveFile();
					}
					catch (OMQFileTransferException e) 
					{
						// TODO Auto-generated catch block
						OMQLogger.getInstance().error("failed to receive file",e);
						
						success = false;
					}	
					OMQLogger.getInstance().info("------------------------------");

				}
				
				// sleep
				OMQLogger.getInstance().info("omq file transfer listener about to sleep "+iSleepTime+" seconds");
				synchronized (sender)
				{
					sender.pause(iSleepTime);
				}
				OMQLogger.getInstance().info("----------------------------------------------------------------");
				OMQLogger.getInstance().info("omq file transfer listener after sleeping "+iSleepTime+" seconds");
				//
			}
		}
		catch (OMQFileTransferInitException e) 
		{
			// TODO Auto-generated catch block
			OMQLogger.getInstance().error("failed to init OMQ file transfer enviroment",e);
			
			success = false;
		}
	
		catch (OMQPropertiesReaderException e) 
		{
			// TODO Auto-generated catch block
			OMQLogger.getInstance().error("failed to read properties file",e);
			
			success = false;
		}
		catch (InterruptedException e) 
		{
			// TODO Auto-generated catch block
			OMQLogger.getInstance().error("OMQ File receive listener was interrupted, see attached excpetion for more info",e);
			
			success = false;
		}
		catch(java.lang.NumberFormatException e)
		{
			OMQLogger.getInstance().error("property omq.ft.sleeptime is not set to a number");
			
			success = false;			
		}
		catch (ServiceException e) 
		{
			// TODO Auto-generated catch block
			OMQLogger.getInstance().error("failed due to ServiceException",e);
			
			success = false;
		}

		
		if (success)
			OMQLogger.getInstance().info("OMQ File Transfer - receive file - ended succesfully");
		else
			OMQLogger.getInstance().info("OMQ File Transfer - receive file - ended with errors");

	}

}
